<?php


namespace app\admin\controller;


use app\common\controller\admin\Base;
use app\admin\model\AppBase;

class Appbaseupdatelog extends Base
{

    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AppBaseUpdateLog();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $page = $this->request->post('page', 1);
            $size = $this->request->post('page_size', 10);
            $sort = $this->request->post('sort', 'id');
            $order = $this->request->post('order', 'desc');
            $adminId = $this->request->post('admin_id', '');
            $appBaseId = $this->request->post('app_base_id', '');

            if ($adminId != '') {
                $where['admin_id'] = $adminId;
            }
            if ($appBaseId != '') {
                $where['app_base_id'] = $appBaseId;
            }
            $where['is_del'] = 0;
            $list = $this->model
                ->field('*')
                ->where($where)
                ->page($page, $size)
                ->order($sort, $order)
                ->select();
            if (!empty($list)) {
                $appBaseAll = AppBase::select()->toArray();
                $appBaseAll = array_column($appBaseAll, null, 'id');
                foreach ($list as $key => $value) {
                    $value['app_base_name'] = empty($appBaseAll[$value['app_base_id']]) ? "默认" : $appBaseAll[$value['app_base_id']]['title'];
                    $list[$key] = $value;
                }
            }
            $total = $this->model->where($where)->count('id');
            $totalPage = ceil($total / $size);
            $data = [
                'total' => $total,
                'list' => $list,
                'totalPage' => $totalPage
            ];
            $this->success('列表', '', $data);
        }
    }
    public function setup($ids = "")
    {
        $type = $this->request->post("type");
        $val = $this->request->post("val");
        $field = $type;
        switch ($type) {
            case 'is_force_update':
                $data[$field] = $val;
                $where['id'] = ['in', $ids];
                break;
            default:
                $this->error('不存在的业务');
                break;
        }
        $this->model->update($data, $where);
        $this->success('操作成功');
    }

}